package com.qingke.bullfight;

import java.io.Serializable;
/**
 * 手牌类
 * @author Administrator
 *
 */
public class Hand implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6390089002166504531L;
	public Poker[] pokers=new Poker[5];//5张扑克牌
	private int[] valueHight=new int[7];//牌力大小
	/**
	 * 
	 */
	public Hand(){
		
	}
	/**
	 * 递归检测
	 * @param count
	 * @param start
	 * @param hand
	 * @param target
	 * @return
	 */
	private boolean judge(int count,int start,Poker[] hand,int target){
		if(target==0&&count==3)
			return true;
		if(start==hand.length)
			return false;
		return judge(count+1,start+1,hand,target-hand[start].getNumber())||judge(count,start+1,hand,target);
	}
	/**
	 * 五小牛
	 * 最大牌型（几率比炸弹还低）
	 * @return
	 */
	public boolean isFiveSmall(){
		int sum=0;
		for(int i=0;i<pokers.length;i++){
			if(pokers[i].getLiteralNum()>=5)
				return false;
			sum+=pokers[i].getLiteralNum();
			if(sum>=10)
				return false;
		}
		return true;
	}
	/**
	 * 返回是否是炸弹
	 * @return
	 */
	public boolean isBomb(){
		int count=0;
		int num=0;
		for(int i=0;i<pokers.length;i++,count=0){
			if(i==pokers.length-1)
				num=pokers[0].getLiteralNum();
			else
				num=pokers[i+1].getLiteralNum();
			for(int j=0;j<pokers.length;j++){
				if(i!=j&&num==pokers[j].getLiteralNum())
					count++;
			}
			if(count==4)
				return true;
		}
		return false;
	}
	/**
	 * 五花牛
	 * 5张花牌
	 * @return
	 */
	public boolean isFiveFaceCards(){
		int count=0;
		for(int i=0;i<pokers.length;i++){
			if(pokers[i].getLiteralNum()>10)
				count++;
		}
		return count==5;
	}
	/**
	 * 四花牛
	 * 4张花牌
	 * @return
	 */
	public boolean isFourFaceCards(){
		int count=0;
		for(int i=0;i<pokers.length;i++){
			if(pokers[i].getLiteralNum()>10)
				count++;
		}
		return count==4;
	}
	/**
	 * 返回是否有牛
	 * @return
	 */
	public boolean isHave(){
		boolean result=judge(0,0,pokers,10)||judge(0,0,pokers,20)||judge(0,0,pokers,30);
		return result;
	}
	/**
	 * 返回牛几
	 * @return
	 */
	public int passPoint(){
		int point=0;
		for(Poker p:pokers){
			point+=p.getNumber();
		}
		point%=10;
		if(point==0)
			point=10;
		return point;
	}
	/**
	 * 返回对应的牛几
	 * @return
	 */
	public String strPoint(){
		switch(passPoint()){
		case 1:
			return "一";
		case 2:
			return "二";
		case 3:
			return "三";
		case 4:
			return "四";
		case 5:
			return "五";
		case 6:
			return "六";
		case 7:
			return "七";
		case 8:
			return "八";
		case 9:
			return "九";
		case 10:
			return "牛";
		default:
			return "";
		}
	}
	/**
	 * 返回最大牌力值
	 * @return
	 */
	public int getHighest(){ 
		int highest=0;
		for(Poker p:pokers){
			highest=Math.max(highest,p.getLiteralNum());
		}
		return highest;
	}
	/**
	 * 返回最大牌
	 * @return
	 */
	public String getHighestPoker(){ 
		int highest=0;
		for(Poker p:pokers){
			highest=Math.max(highest,p.getValue());
		}
		return Poker.valueToString(highest);
	}
	/**
	 * 根据5张牌设置牌力
	 */
	public void setValueHight(){
		if(isFiveSmall()){
			valueHight[0]=1;
			valueHight[1]=getHighest();
			return ;
		}
		else
			valueHight[0]=0;
		
		if(isBomb()){
			valueHight[1]=1;
			valueHight[2]=pokers[0].getLiteralNum()==pokers[1].getLiteralNum()?pokers[0].getLiteralNum():pokers[2].getLiteralNum();
			return ;
		}
		else
			valueHight[1]=0;
		
		if(isHave()&&isFiveFaceCards())
			valueHight[2]=1;
		else
			valueHight[2]=0;
		
		if(isHave()&&isFourFaceCards())
			valueHight[3]=1;
		else
			valueHight[3]=0;
		
		if(isHave()){
			valueHight[4]=1;
			valueHight[5]=passPoint();
			valueHight[6]=getHighest();
		}
		else{
			valueHight[4]=0;
			valueHight[5]=0;
			valueHight[6]=getHighest();
		}
	}
	/**
	 * 与另一副手牌比较，胜负平
	 * @param other
	 * @return
	 */
	public Result compare(Hand other){
		this.setValueHight();
		other.setValueHight();
		for(int i=0;i<valueHight.length;i++){
			if(valueHight[i]>other.valueHight[i]) 
				return Result.WIN;
			if(valueHight[i]<other.valueHight[i])
				return Result.LOSE;
		}
		return Result.TIE;
	}
	/**
	 * 返回手牌信息
	 * @return
	 */
	public String show(){
		String str="手牌：";
		for(Poker p:pokers){
			str+=p.toString();
		}
		return str;
	}
	/**
	 * 返回手牌和牌力信息
	 */
	public String toString(){
		String str="";
		str+=show();
		str+="      ";
		if(isFiveSmall()){
			str+="五小牛，";
		}
		else if(isBomb()){
			str+="炸弹，";
		}
		else if(isFiveFaceCards()){
			str+="五花牛，";
		}
		else if(isFourFaceCards()){
			str+="四花牛，";
		}
		else if(isHave()){
			str+="有牛，牛"+strPoint()+"，";
		}
		else{
			str+="没牛，";
		}
		str+="最大"+getHighestPoker();
		return str;
	}
}
